#define BASE_TONE_LEN  100 // msec
#define ON_TIME 80      // Percent

unsigned int NOTE1[] = {0x3C08,0x3E08,0x4008,0x3C08,0x3C08,0x3E08,0x4008,0x3C08};
unsigned int NOTE2[] = {0x4008,0x4108,0x4210,       0x4008,0x4108,0x4210};
#define MAX_NOTES_CNT 8


//Note No. Hex
// C1  24  18
// C2  36  24
// C3  48  30
// C4  60  3C
// C5  72  48
// C6  84  54
// C7  96  60
// C8 108  6C
// C9 120  78
// 001 0x01   B00001 32分音符
// 002 0x02   B00010 16分音符
// 004 0x04   B00100 8分音符
// 008 0x08   B01000 4分音符
// 016 0x10   B10000 2分音符


int n1_cnt = 0;
int n2_cnt = 0;

int n1_th;
int n2_th;

int n1_pt = -1 ;
int n2_pt = -1;


void setup() {

}


void loop () {
  int n1_tone;
  int n1_len;
  int n2_tone;
  int n2_len;

  n1_cnt--;
  n2_cnt--;
  
  if ( n1_cnt < 0 ) {  // goto next note.
    n1_pt++ ;
    if (n1_pt >= MAX_NOTES_CNT ) {
      n1_pt = 0 ;   // start from top.
    }
    n1_len  = NOTE1[n1_pt] & 0x00FF ;
    n1_cnt = n1_len * BASE_TONE_LEN ;
    n1_th  = n1_cnt - (n1_cnt * ON_TIME / 100);

    n1_tone = NOTE1[n1_pt] >> 4 & 0x00FF ;
    NOTE_ON (1,n1_tone);  // ###############
  }
  
  if ( n2_cnt < 0 ) {  // goto next note.
    n2_pt++ ;
    if (n2_pt >= MAX_NOTES_CNT ) {
      n2_pt = 0 ;   // start from top.
    }
    n2_len  = NOTE2[n2_pt] & 0x00FF ;
    n2_cnt = n2_len * BASE_TONE_LEN ;
    n2_th  = n2_cnt - (n2_cnt * ON_TIME / 100);

    n2_tone = NOTE2[n2_pt] >> 4 & 0x00FF ;
    NOTE_ON(2, n2_tone);  // ###############
  }
  
  if ( n1_cnt < n1_th ) {
     NOTE_OFF(1);
  }
  
  
  if ( n2_cnt < n2_th ) {
     NOTE_OFF(2);
  }
  delay(1);
}

void NOTE_ON (int machine, int tone )
{
  //
}
void NOTE_OFF (int machine)
{
  //
}







